package com.dly.blog.controller;

import com.dly.blog.annotation.IgnoreAccess;
import com.dly.blog.domain.ro.FileRO;
import com.dly.blog.domain.vo.FileVO;
import com.dly.blog.msg.R;
import com.dly.blog.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.plugin.core.config.EnablePluginRegistries;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: DaiLiYu
 * @Date: 2023/7/11 22:26
 */
@RestController
@RequestMapping("/file")
@Tag(name = "文件处理")
public class FileController extends BaseController {

    @Resource
    private FileService fileService;


    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    @PreAuthorize("hasAuthority('file:file:upload')")
    @Operation(summary = "文件上传")
    public ResponseEntity<R<FileVO>> upload(@Valid FileRO fileRo, BindingResult br) {
        return operate(() -> this.successDate(fileService.save(fileRo)), br);
    }

    @GetMapping(value = "/loadFileId")
    @Operation(summary = "文件加载")
    @Parameter(name = "fileId", description = "文件id")
    @IgnoreAccess
    public ResponseEntity<R<Void>> fileIdLoad(HttpServletResponse response, String fileId){
        fileService.fileIdLoad(response, fileId);
        return operate(this::success);
    }

    @GetMapping(value = "/fileTokenLoad")
    @Operation(summary = "文件通过Token加载")
    @Parameter(name = "token", description = "Token")
    @IgnoreAccess
    public ResponseEntity<R<Void>> fileTokenLoad(HttpServletResponse response, String token){
        fileService.fileTokenLoad(response, token);
        return operate(this::success);
    }
}
